package com.rui.study.demo.httpclient;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: study_20180903
 * @description:
 * @author: Yaowr
 * @create: 2018-10-29 10:23
 **/
public class MyElasticJob implements SimpleJob, ElasticJobListener {

    static JobScheduler scheduler;
    static CoordinatorRegistryCenter regCenter;
    static LiteJobConfiguration simpleJobRootConfig;

    public static void main(String[] args) {
        scheduler = new JobScheduler(createRegistryCenter(), createJobConfiguration(), new MyElasticJob());
        scheduler.init();
    }

    private static CoordinatorRegistryCenter createRegistryCenter() {
        regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration("172.16.5.16:2181", "elastic-job-demo"));
        regCenter.init();

        List<String> children = regCenter.getChildrenKeys("/");
        for (String child : children) {
            regCenter.remove("/" + child);
        }
        return regCenter;
    }


    /**
     * 创建作业配置
     * @return
     */
    private static LiteJobConfiguration createJobConfiguration() {

        // 定义作业核心配置
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder("101", "0 49 14 30 10 ?", 1).build();
        // 定义SIMPLE类型配置
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, MyElasticJob.class.getCanonicalName());
        // 定义Lite作业根配置
        simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build();
        return simpleJobRootConfig;
    }


    @Override
    public void execute(ShardingContext context) {

        String threadName = Thread.currentThread().getName();
        String jobName = context.getJobName();
        String taskId = context.getTaskId();
        int shardingItem = context.getShardingItem();

        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        System.out.println(time + "：[" + threadName + "]---[" + jobName +"]---[" + taskId + "]---[" + shardingItem + "]");
    }

    @Override
    public void beforeJobExecuted(ShardingContexts shardingContexts) {
        System.out.println("Before " + shardingContexts.getJobName());
    }

    @Override
    public void afterJobExecuted(ShardingContexts shardingContexts) {
        String jobName = shardingContexts.getJobName();
        System.out.println("After " + jobName);
        regCenter.remove("/" + jobName);
    }
}
